﻿#include "qquickvncviewer.h"
#include <QDebug>
#include <QPainter>

QQuickVncViewer::QQuickVncViewer(QQuickItem *parent):
    QQuickPaintedItem(parent),
    m_host(""),
    m_port(0),
    m_password(""),
    m_recvDisConnect(false)
{
    // By default, QQuickItem does not drainw anythg. If you subclass
    // QQuickItem to create a visual item, you will need to uncomment the
    // following line and re-implement updatePaintNode()

    // setFlag(ItemHasContents, true);

    connect(&m_viewerAdapter, SIGNAL(updateRect(QRect)), this, SLOT(onUpdateRect(QRect)), Qt::QueuedConnection);
    connect(&m_viewerAdapter, SIGNAL(onPropChanged()), this, SLOT(handlePropChanged()), Qt::QueuedConnection);
    connect(&m_viewerAdapter, SIGNAL(error(const QString)), this, SIGNAL(error(const QString)), Qt::QueuedConnection);
    connect(&m_viewerAdapter, SIGNAL(disConnect()), this, SLOT(onDisconncet()), Qt::QueuedConnection);
    connect(this, SIGNAL(error(const QString)), this, SLOT(onError(const QString)));
    setAcceptedMouseButtons(Qt::AllButtons);
    setAcceptHoverEvents(true);
    setRenderTarget(QQuickPaintedItem::FramebufferObject);
    setOpaquePainting(false);

    //connect(&m_viewerAdapter, SIGNAL(established()), this, SIGNAL(established()));
}

QQuickVncViewer::~QQuickVncViewer()
{
    qDebug()<< "===================~QQuickVncViewer";
}

QString QQuickVncViewer::host() const
{
    return m_host;
}

void QQuickVncViewer::setHost(const QString &value)
{
    m_host = value;
}

int QQuickVncViewer::port() const
{
    return m_port;
}

void QQuickVncViewer::setPort(const int &value)
{
    m_port = value;
}

QString QQuickVncViewer::password() const
{
    return m_password;
}

void QQuickVncViewer::setPassword(const QString &value)
{
    m_password = value;
}

int QQuickVncViewer::getServerWidth()
{
    return m_viewerAdapter.width();
}

int QQuickVncViewer::getServerHeight()
{
    return m_viewerAdapter.height();
}

float QQuickVncViewer::getImageScale()
{
    if(m_viewerAdapter.width() > 1930)
        return 0.64;
    else if(m_viewerAdapter.width() < 1920)
        return 0.85;
}

void QQuickVncViewer::paint(QPainter *painter)
{
    if(!m_viewerAdapter.bufferImage())
        return;
    QRect rect = painter->clipBoundingRect().toRect();
    rect = m_viewerAdapter.bufferImage()->rect().intersected(rect);
    painter->drawImage(rect.topLeft(), m_viewerAdapter.bufferImage()->copy(rect).convertToFormat(QImage::Format_ARGB32));
}

void QQuickVncViewer::onError(const QString errorString)
{
    //qDebug()<< "======================" <<errorString;
}

void QQuickVncViewer::onDisconncet()
{
    qDebug()<< "======================" <<"连接已断开";
    m_recvDisConnect = true;
    emit error(QStringLiteral("连接已断开"));
}

void QQuickVncViewer::start()
{
    m_viewerAdapter.start(m_host, m_port, m_password);
}

void QQuickVncViewer::stop()
{
    m_viewerAdapter.stop();
}

void QQuickVncViewer::refresh()
{
    m_viewerAdapter.refresh();
}

bool QQuickVncViewer::eventMyKey(int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode,
                                 const QString &text , bool autorep, int count)
{
     //qDebug()<<"5555555555555555555555555"<<key<<modifiers<<nativeScanCode<<text<<autorep<<count;
     QKeyEvent keyEvent(QEvent::KeyPress,key, modifiers, text, autorep, count);
     m_viewerAdapter.keyEventHandler(&keyEvent);
     return keyEvent.isAccepted();
}

void QQuickVncViewer::eventMyKey(const int myKey, const QString text, const bool pressed)
{
    m_viewerAdapter.keyEventHandler(myKey, text, pressed);
}

bool QQuickVncViewer::event(QEvent *event)
{
    if(m_recvDisConnect)  //断开连接了
        return false;
   // qDebug()<<"================"<<event->type();
    switch (event->type()) {
    case QEvent::KeyPress:
    case QEvent::KeyRelease:
        m_viewerAdapter.keyEventHandler(static_cast<QKeyEvent*>(event));
        return event->isAccepted();
    case QEvent::MouseButtonDblClick:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseMove:
        m_viewerAdapter.mouseEventHandler(static_cast<QMouseEvent*>(event));
        return true;
    case QEvent::HoverMove:
        m_viewerAdapter.hoverEventHandler(static_cast<QHoverEvent*>(event));
        return true;
    case QEvent::Wheel:
        m_viewerAdapter.wheelEventHandler(static_cast<QWheelEvent*>(event));
        return true;
    default:
        return QQuickItem::event(event);
    }
}

void QQuickVncViewer::onUpdateRect(const QRect rect)
{
    update(rect);
}

void QQuickVncViewer::handlePropChanged()
{
    setWidth(m_viewerAdapter.width());
    setHeight(m_viewerAdapter.height());
    update();

    emit propChanged();
}
